<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "category".
 *
 * @property integer $id
 * @property string $name
 * @property string $pinyin_name
 * @property integer $parent_id
 * @property string $img
 * @property string $des
 * @property integer $level
 * @property integer $sort_number
 * @property integer $is_show
 * @property string $create_time
 * @property string $update_time
 */
class Category extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'category';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['parent_id', 'level', 'sort_number', 'is_show'], 'integer'],
            [['des'], 'string'],
            [['create_time', 'update_time'], 'safe'],
            [['name'], 'string', 'max' => 50],
            [['pinyin_name'], 'string', 'max' => 100],
            [['img'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '分类名称',
            'pinyin_name' => '名称-拼音',
            'parent_id' => '上级id',
            'img' => '图片',
            'des' => '描述',
            'level' => '层级',
            'sort_number' => '排序',
            'is_show' => '显示状态' ,//1、显示2、隐藏',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
        ];
    }

    public static function itemAlias ($type, $code = NULL)

    {
        $_items = [            
            'is_show' => [
                //''  =>'显示状态',
                '1' => "显示",              
                '2' => "隐藏",              
            ],
        ];

        if (isset($code))
            return isset($_items[$type][$code]) ? $_items[$type][$code] : false;
        else
            return isset($_items[$type]) ? $_items[$type] : false;
    }

    //父级id列表
    public static function listid ($is_show = 0){

        if($is_show == 0) {
            $where = [];
        }  else {
            $where = ['is_show' => $is_show];
        }
        $li = [];
        $list = Category::find()->select('id,name')->where($where)->orderBy('id ASC')->asarray()->all();

        if($list){

            $li[0] = "最高级";
            foreach ($list as $k => $v) {
                $li[$v['id']] = $v['name'];
            }    
        }
        
        //var_dump($list);
        return $li;
    }

    /*
    * 保存前执行的操作
    */
    public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
            //是否是新添加
            if($this->isNewRecord) {
                if(!$this->is_show) $this->is_show = 1;
                if(!$this->sort_number) $this->sort_number = 999;
                if(!$this->level) $this->level = 1;
                if(!$this->parent_id) $this->parent_id = 0;
                
                $this->create_time = date("Y-m-d H:i:s", time());
                $this->update_time = date("Y-m-d H:i:s", time());
            }else{
                $this->update_time = date("Y-m-d H:i:s", time());
            }
            return true;
        } else {
            return false;
        }
    } 
}
